/**
 * Created by RickyTT on 14-1-25.
 */

var should = require('should');
var assert = require('assert');
var UserInfo = require('../model/userInfo.js');
var User = require('../model/user.js');
var UserCollection = require('../model/userCollection.js');
var Post = require('../model/post.js');
var Promise = require('promise');
var lib = require('../lib/lib.js');
var helper = require('../lib/appHelper.js');


describe('User',function(){

    var userId;

    describe('User insert',function(){
        it('insert name = 2222,password = 123456',function(done){
            User.insert('1223ad','123456',function(err,result){
                assert(err === null,'User insert err');
                result.insertId.should.be.number;
                result.insertId.should.be.above(1);
                userId = result.insertId;
//                console.log(userId);
                done();
            });
        });
    });

    describe('User getById',function(){
        it('Get User By id = 15',function(done){
            User.getById(15,function(err,user){

                assert(err === null,'User getById err');
                //err.should.equal(1);
                user.id.should.equal(15);
//                console.log(user);
                done();
            });
        });

        it('Get User by id = 11,should not be searched',function(done){
            User.getById(11,function(err,result){
                assert(err == null,'User getById = 11 err');
                assert(result.length < 1,'result is not null');
                done();
            })
        });
    });

    describe('User getAll',function(){
        it('Get all',function(done){
            User.getAll(function(err,result){
//                console.log(result);
                assert(err === null,'User get all err');
                result.length.should.be.above(0);
                done()
            });
        });
    });

    describe('User update password',function(){
        it('Update User password to abcdef by id = 14',function(done){
            User.update('abcdefdg',14,function(err){
//                console.log(err);
                err.should.equal(1);
                done();
            });
        });
    });

    describe('User delete by id',function(){
        it('delete by'+userId,function(done){
            User.deleteById(userId,function(err,result){
                assert(err === null,'User delete err');
                console.log(result);
                done();
            });
        });
    });


    describe('User getByName',function(){
        it('Get User By Name = abassc',function(done){
            User.getByName('abassc',function(err,result){
                assert(err === null,'get by name is err');
                result.name.should.equal('abassc');
                done();
            });
        });
     });


    describe('User getByNameAndPassword',function(){
        it('Get User by name = abassc and password = abcdefdg',function(done){
            User.getByNameAndPassword('abassc','abcdefdg',function(err,result){
                assert(err === null,'get by name and password is err');
                result.name.should.equal('abassc');
                result.password.should.equal('abcdefdg');
                done();
            })
        })
    })
});


describe('UserInfo',function(){
    var userId = 15;

    before(function(){
        User.getAll(function(err,result){
            var user = result[result.length];
            console.log('user '+user);
            //userId = user.id;
        });
    });

    describe('UserInfo insert',function(){
        it('insert userId = '+userId,function(done){
            UserInfo.insert(14,1,'ibaby@163.com',1,'2013-01-28',function(err,result){
//                console.log(result);
                assert(err === null,'UserInfo insert err');
                done();
            });
        });
    });

    describe('UserInfo delete by userId',function(){
        it('delete userId',function(done){
            UserInfo.deleteByUserId(14,function(err,result){
                assert(err === null,'UserInfo delete err');
                done();
            });
        });
    });
});


describe('UserCollection',function(){
    describe('get collection = 15',function(){
        it('get',function(done){
            UserCollection.getById(15,function(err,userCollection){
                console.log(userCollection);
                userCollection.should.be.not.empty;
                userCollection.should.be.an.object;
                userCollection.user.id.should.equal(15);
                done();
            });
        });
    });
});


describe('Post',function(){
    var id;
    it('Insert',function(done){
        Post.insert(43,'abc','好难搞得！！',function(err,result){
            assert(err == null,'post insert err');
            id = result.insertId;
            done();
        });
    });

    it('select num',function(done){
        Post.getByNum(5,function(err,result){
            assert(err == null,'post get by num err');
            result.length.should.be.below(6);
            done();
        });
    });

    it('delete id = '+id,function(done){
        Post.deleteById(id,function(err,result){
            assert(err == null,'post detele err');
            done();
        })
    })
});


describe('lib test',function(){
    it('md5 test password = abc',function(done){
        console.log(lib.MD5('abc'));
        done();
    });

    it('sex',function(done){
        var sex = helper.Sex.female.name;
        console.log(sex);
        done();
    });
});